package com.simple.portal.bo.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.portal.bo.IGroupInfoBO;
import com.simple.portal.dao.IGroupInfoDAO;
import com.simple.portal.dao.IGroupRightRelDAO;
import com.simple.portal.dao.IInitRightDAO;
import com.simple.portal.vo.GroupInfoVO;
import com.simple.portal.vo.GroupRightRelVO;
import com.simple.portal.vo.InitRightVO;
import com.simple.portal.vo.OrganizationVO;
import com.simple.util.common.Constants;
import com.simple.util.common.DateUtil;
import com.simple.util.object.PageInfoBean;
import com.simple.util.object.portal.GroupInfoBean;
import com.simple.util.object.portal.SessionUserBean;

@Service("groupInfoBO")
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public class GroupInfoBOImpl implements IGroupInfoBO {
	
	@Autowired
	private IGroupInfoDAO groupInfoDAO;
	@Autowired
	private IInitRightDAO initRightDAO;
	@Autowired
	private IGroupRightRelDAO groupRightRelDAO;
	
	@Override
	public GroupInfoVO findById(String id) {
		
		return groupInfoDAO.findById(id);
	}

	@Override
	public List<GroupInfoVO> findAll() {
		
		return groupInfoDAO.findAll();
	}
	
	@Override
	public List<GroupInfoBean> findGroupInfoByPage(final String userId,final String groupName,final String organizationId,final  String status,final PageInfoBean pageInfoBean){
		
		final List<?> groupInfoBeanList = groupInfoDAO.findGroupInfoByPage(userId, groupName, organizationId, status, pageInfoBean);
		final List<GroupInfoBean> tempGroupInfoBeanList = new ArrayList<GroupInfoBean>();
 
		for (int i = 0; i < groupInfoBeanList.size(); i++) {
			final GroupInfoBean groupInfoBean = new GroupInfoBean();
			Object [] objectArray =(Object[])groupInfoBeanList.get(i);
		    groupInfoBean.setGroupInfoVO((GroupInfoVO)objectArray[0]);
	    	groupInfoBean.setOrganizationVO((OrganizationVO) objectArray[1]);
			tempGroupInfoBeanList.add(groupInfoBean);
		}
		return tempGroupInfoBeanList;
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void addGroupInfo(final GroupInfoVO groupInfoVO,final SessionUserBean sessionUserBean){
		
			String type=sessionUserBean.getOrganizationVO().getOrganizationType();

			groupInfoVO.setAdminFlag(Constants.STATE_NO);
			groupInfoVO.setOrganizationId(sessionUserBean.getOrganizationVO().getOrganizationId());
			groupInfoVO.setCreator(sessionUserBean.getUserInfoVO().getUserId());
			groupInfoVO.setCreateDate(DateUtil.getNowDate());
			groupInfoVO.setUpdater(sessionUserBean.getUserInfoVO().getUserId());
			groupInfoVO.setUpdateDate(DateUtil.getNowDate());
			GroupInfoVO tempGroupInfoVO=null;
			//adding the information of the group and adding default rights
			if(Constants.ORGANIZATION_TYPE_SYSTEM.equals(type)){
				groupInfoVO.setGroupLevel(Constants.GROUP_LEVEL_THREE);
				tempGroupInfoVO=groupInfoDAO.save(groupInfoVO,sessionUserBean.getUserInfoVO().getUserId());
				
				List <InitRightVO> initRightVOList=initRightDAO.findInitRightVOList(Constants.ORGANIZATION_TYPE_SYSTEM, Constants.STATE_NO);

				for(InitRightVO initRightVO :initRightVOList){
					GroupRightRelVO groupRightRelVO =new GroupRightRelVO();
					groupRightRelVO.setGroupId(tempGroupInfoVO.getGroupId());
					groupRightRelVO.setRightId(initRightVO.getId().getRightId());
					groupRightRelVO.setRightFlag(initRightVO.getId().getRightFlag());
					groupRightRelDAO.save(groupRightRelVO, sessionUserBean.getUserInfoVO().getUserId());
				}
				
			}else if(Constants.ORGANIZATION_TYPE_ENTERPRISE.equals(type)){
				groupInfoVO.setGroupLevel(Constants.GROUP_LEVEL_ONE);
				tempGroupInfoVO=groupInfoDAO.save(groupInfoVO,sessionUserBean.getUserInfoVO().getUserId());
				List <InitRightVO> initRightVOList=initRightDAO.findInitRightVOList(Constants.ORGANIZATION_TYPE_ENTERPRISE, Constants.STATE_NO);

				for(InitRightVO initRightVO :initRightVOList){
					GroupRightRelVO groupRightRelVO =new GroupRightRelVO();
					groupRightRelVO.setGroupId(tempGroupInfoVO.getGroupId());
					groupRightRelVO.setRightId(initRightVO.getId().getRightId());
					groupRightRelVO.setRightFlag(initRightVO.getId().getRightFlag());
					groupRightRelDAO.save(groupRightRelVO, sessionUserBean.getUserInfoVO().getUserId());
				}
				
			}else if(Constants.ORGANIZATION_TYPE_PARTNER.equals(type)){
				groupInfoVO.setGroupLevel(Constants.GROUP_LEVEL_ONE);			
				tempGroupInfoVO=groupInfoDAO.save(groupInfoVO,sessionUserBean.getUserInfoVO().getUserId());
				List <InitRightVO> initRightVOList=initRightDAO.findInitRightVOList(Constants.ORGANIZATION_TYPE_PARTNER, Constants.STATE_NO);

				for(InitRightVO initRightVO :initRightVOList){
					GroupRightRelVO groupRightRelVO =new GroupRightRelVO();
					groupRightRelVO.setGroupId(tempGroupInfoVO.getGroupId());
					groupRightRelVO.setRightId(initRightVO.getId().getRightId());
					groupRightRelVO.setRightFlag(initRightVO.getId().getRightFlag());
					groupRightRelDAO.save(groupRightRelVO, sessionUserBean.getUserInfoVO().getUserId());
				}
				
			}
			

		
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void editGroupInfo(final GroupInfoVO groupInfoVO,final SessionUserBean sessionUserBean){
		
		GroupInfoVO tempGroupInfoVO =findById(groupInfoVO.getGroupId());
		
		tempGroupInfoVO.setGroupName(groupInfoVO.getGroupName());
		tempGroupInfoVO.setMemo(groupInfoVO.getMemo());
		tempGroupInfoVO.setStatus(groupInfoVO.getStatus());
		tempGroupInfoVO.setUpdater(sessionUserBean.getUserInfoVO().getUserId());
		tempGroupInfoVO.setUpdateDate(DateUtil.getNowDate());
		
		groupInfoDAO.update(tempGroupInfoVO,Constants.LOGGER_OPERATE_FLAG_UPDATE,sessionUserBean.getUserInfoVO().getUserId());
		
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void controlGroupInfo(final String  groupId,final String status,final SessionUserBean sessionUserBean){
		String operateFlag=Constants.STATE_NORMAL.equals(status) ? Constants.LOGGER_OPERATE_FLAG_START:Constants.LOGGER_OPERATE_FLAG_STOP;
		GroupInfoVO tempGroupInfoVO =findById(groupId);
		tempGroupInfoVO.setStatus(status);
		tempGroupInfoVO.setUpdater(sessionUserBean.getUserInfoVO().getUserId());
		tempGroupInfoVO.setUpdateDate(DateUtil.getNowDate());
		groupInfoDAO.update(tempGroupInfoVO,operateFlag,sessionUserBean.getUserInfoVO().getUserId());
		
	}

	@Override
	public List<GroupInfoVO> findByCreator(String userId) {
		return groupInfoDAO.findByCreator(userId);
	}

}
